/**
 * 动态调整路由表 TBD
 */

import {RouteRecordRaw, createRouter, createWebHashHistory} from 'vue-router';
import routerMap from "./constantRouterMap";

import {useStorePermission} from "@store/permission";
import {useStoreUser} from "@store/user";

const {GenerateRoutes, routers, checkRoute} = useStorePermission();
const {hasLogin, roles, logout} = useStoreUser();

const whiteList = ["/", '/login', '/regin', '/landing'] // 不重定向白名单(根路径、登陆路径、注册路径、着陆页），根路径必须在白名单内
let accessRoutes: any = [];
//已经登陆，直接根据权限判断路径
if (hasLogin) {
	const hasRoles = roles && roles.length > 0;//是否有角色设定
	//已登陆但无角色的，仍视为游客
	accessRoutes = await GenerateRoutes(roles);
} else {
	// 未登陆，仅显示游客权限相关
	accessRoutes = await GenerateRoutes([]);
}
//在白名单内，直接放行
routerMap.forEach(route => {
	if (whiteList.includes(route.path)) {
		accessRoutes.push(route)
	}
});

export default createRouter({
	history: createWebHashHistory(),
	routes: accessRoutes
})
